//
//  GSSubscribeViewController_Add_Mine.m
//  GameSky
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 NiRongfei. All rights reserved.
//

#import "GSSubscribeViewController_Add_Mine.h"
#import "GSSubscribeViewController_Add_Topic_Details.h"
#import "GSSubscribeViewController_Add_Topic_Cell.h"
#import "GSSubscrebe_Add_Topic.h"

@interface GSSubscribeViewController_Add_Mine () <UITableViewDelegate,UITableViewDataSource>
// 展示页面
@property (nonatomic, strong) UITableView *tableView;
/**
 * 数据库
 */
@property (nonatomic, strong) FMDatabase *db;
// 数据源数组
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation GSSubscribeViewController_Add_Mine

#pragma mark-------------视图加载完成-------------------
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 数据
    [self setUpData];
    // 添加tableView
    [self addTableView];

}

#pragma mark------------数据源数组懒加载---------------
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
#pragma mark------------解析数据-----------------------
- (void)setUpData{
    // 从数据库里获取 数据
    // 打开数据库
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    // 拼接路径
    NSString *pathString = [[path stringByAppendingPathComponent:@"data"]stringByAppendingPathComponent:@"GSDataBase.db"];
    _db = [FMDatabase databaseWithPath:pathString];
    if (![_db open]) {
        NSLog(@"数据打开失败");
    }
    [self.dataArray removeAllObjects];
 // 取出 存储的 数据 展示
    FMResultSet *resultSet = [_db executeQuery:@"select * from SubscribeList"];
    while ([resultSet next]) {
        NSString *nodeIds = [resultSet stringForColumn:@"nodeIds"];
        NSNumber *cnt = [NSNumber numberWithInt:[resultSet intForColumn:@"cnt"]];
        NSString *sourceName = [resultSet stringForColumn:@"sourceName"];
        NSData *imageData = [resultSet dataForColumn:@"imageData"];
        GSSubscrebe_Add_Topic *model = [[GSSubscrebe_Add_Topic alloc] init];
        model.sourceName = sourceName;
        model.imageModel = [UIImage imageWithData:imageData];
        model.sourceId = nodeIds;
        model.cnt = cnt;
        model.isSubscribe = YES;
        [self.dataArray addObject:model];
    }    
}
#pragma mark-------------添加tableView----------------
- (void)addTableView{
    // 创建
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:(UITableViewStylePlain)];
    // 夜间模式
    _tableView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
    // 代理
    _tableView.dataSource = self;
    // 设置行高
    _tableView.rowHeight = 72;
    _tableView.delegate = self;
    // 添加视图
    [self.view addSubview:_tableView];
    // 添加 表尾
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,kScreenWidth, 48)];
    footerView.backgroundColor = [UIColor grayColor];
    UILabel *footLabel = [[UILabel alloc] initWithFrame:CGRectMake(6, 6, kScreenWidth - 12, 36)];
    footLabel.backgroundColor = [UIColor whiteColor];
    [footerView addSubview:footLabel];
    footLabel.textColor = [UIColor redColor];
    footLabel.textAlignment = NSTextAlignmentCenter;
    footLabel.text = @"没有更多内容了";
    
    _tableView.tableFooterView = footerView;
}
#pragma mark---------------tableView协议方法------------
// 返回多少分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
// 每个分区多少行Cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
// 创建Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"myCell";
    GSSubscribeViewController_Add_Topic_Cell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[GSSubscribeViewController_Add_Topic_Cell alloc] init];
    }
    GSSubscrebe_Add_Topic *model = _dataArray[indexPath.row];
    cell.model = model;
    cell.subscibeButton.tag = indexPath.row;
    [cell.subscibeButton addTarget:self action:@selector(subscibeButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    // 设置选中样式
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
// 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 获取model
     GSSubscrebe_Add_Topic *model = _dataArray[indexPath.row];
    // 创建 请求字典, 添加 相应键值对, 传给下一个界面,方便 请求数据
    NSDictionary *dic = @{@"request":@{@"parentNodeId":@"dingYue",
                                       @"nodeIds":model.sourceId,
                                       @"type":@"dingyueList",
                                       @"pageIndex":@1,
                                       @"elementsCountPerPage":@20}
                          };
    // 协议~~~~~~
    if ([_delegate respondsToSelector:@selector(pushToNextControllerWithDic:isSubscribe:model:imageData:)]) {
        [_delegate pushToNextControllerWithDic:dic isSubscribe:YES model:model imageData:UIImagePNGRepresentation(model.imageModel)];
    }
}
#pragma mark------------取消订阅按钮-------------------
- (void)subscibeButtonAction:(UIButton *)button{
    // 打开数据库
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    // 拼接路径
    NSString *pathString = [[path stringByAppendingPathComponent:@"data"]stringByAppendingPathComponent:@"GSDataBase.db"];
    _db = [FMDatabase databaseWithPath:pathString];
    if (![_db open]) {
        NSLog(@"数据打开失败");
    }

    // 取出model
    GSSubscrebe_Add_Topic *model = _dataArray[button.tag];
    
     [self.dataArray removeObject:model];
    [_tableView reloadData];
    
    if (![_db executeUpdate:@"delete from SubscribeList where nodeIds = ?",model.sourceId]) {
        NSLog(@"删除数据失败");
        
    }
}


#pragma mark----------- 改变  订阅按钮状态-----------

- (void)changeSubscribe{
    [self setUpData];
    [_tableView reloadData];
}
#pragma mark------------------------------------------
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
